﻿local curl = require "lcurl.safe"

script_info = {
    ["title"] = "KinhDownload",
    ["description"] = "百度网盘kinh下载脚本，https://www.kinh.top，不限速",
    ["version"] = "0.0.2",
    ["color"] = "66ccff",
}

function onInitTask(task, user, file)
	if task:getType() ~= TASK_TYPE_SHARE_BAIDU then
		return false
	end
	if user == nil then
        task:setError(-1, "用户未登录")
		return true
	end
	local data = ""
	local header = {"User-Agent: netdisk;2.0.8.0","Cookie: "..user:getCookie()}
	local c = curl.easy {
        url = file.dlink,
		httpheader = header,
		ssl_verifyhost = 0,
		ssl_verifypeer = 0,
		timeout = 15,
		proxy = pd.getProxy(),
		writefunction = function(buffer)
			data = data..buffer
			return #buffer
		end,
	}
	if header ~= nil then
		c:setopt(curl.OPT_HEADER, true)
	end
	local _, e = c:perform()
	c:close()
	if e then
		return false
	end
	local d_start=string.find(data,"Location: http")+10
	local d_end =string.find(data,"&by=themis")+9
    local d_link=string.sub(data,d_start,d_end)
	task:setUris(d_link)
	task:setOptions("user-agent", "netdisk;2.0.8.0")
	if string.find(d_link, "https://qdall01.baidupcs.com/file") then
		task:setIcon("icon/limit_rate.png", "高速通道受限")
	else
		task:setIcon("icon/accelerate.png", "高速通道加速中")
	end
	return true
end